﻿//============================================================
//http://net.itcast.cn author:yangzhongke
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using OpenLab.Model;

namespace OpenLab.DAL
{
	public partial class TubeDAL
	{
        public Tube Add
			(Tube tube)
		{
				string sql ="INSERT INTO Tube (abriviation, designation)  output inserted.id VALUES (@abriviation, @designation)";
				SqlParameter[] para = new SqlParameter[]
					{
						new SqlParameter("@abriviation", ToDBValue(tube.Abriviation)),
						new SqlParameter("@designation", ToDBValue(tube.Designation)),
					};
					
				int newId = (int)SqlHelper.ExecuteScalar(sql, para);
				return GetById(newId);
		}

        public int DeleteById(int id)
		{
            string sql = "DELETE Tube WHERE Id = @Id";

           SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};
		
            return SqlHelper.ExecuteNonQuery(sql, para);
		}
		
				
        public int Update(Tube tube)
        {
            string sql =
                "UPDATE Tube " +
                "SET " +
			" abriviation = @abriviation" 
                +", designation = @designation" 
               
            +" WHERE id = @id";


			SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@id", tube.Id)
					,new SqlParameter("@abriviation", ToDBValue(tube.Abriviation))
					,new SqlParameter("@designation", ToDBValue(tube.Designation))
			};

			return SqlHelper.ExecuteNonQuery(sql, para);
        }		
		
        public Tube GetById(int id)
        {
            string sql = "SELECT * FROM Tube WHERE Id = @Id";
            using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql, new SqlParameter("@Id", id)))
			{
				if (reader.Read())
				{
					return ToModel(reader);
				}
				else
				{
					return null;
				}
       		}
        }
		
		public Tube ToModel(SqlDataReader reader)
		{
			Tube tube = new Tube();

			tube.Id = (int)ToModelValue(reader,"id");
			tube.Abriviation = (string)ToModelValue(reader,"abriviation");
			tube.Designation = (string)ToModelValue(reader,"designation");
			return tube;
		}
		
		public int GetTotalCount()
		{
			string sql = "SELECT count(*) FROM Tube";
			return (int)SqlHelper.ExecuteScalar(sql);
		}
		
		public IEnumerable<Tube> GetPagedData(int minrownum,int maxrownum)
		{
			string sql = "SELECT * from(SELECT *,row_number() over(order by id) rownum FROM Tube) t where rownum>=@minrownum and rownum<=@maxrownum";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql,
				new SqlParameter("@minrownum",minrownum),
				new SqlParameter("@maxrownum",maxrownum)))
			{
				return ToModels(reader);					
			}
		}
		
		public IEnumerable<Tube> GetAll()
		{
			string sql = "SELECT * FROM Tube";
			using(SqlDataReader reader = SqlHelper.ExecuteDataReader(sql))
			{
				return ToModels(reader);			
			}
		}
		
		protected IEnumerable<Tube> ToModels(SqlDataReader reader)
		{
			var list = new List<Tube>();
			while(reader.Read())
			{
				list.Add(ToModel(reader));
			}	
			return list;
		}		
		
		protected object ToDBValue(object value)
		{
			if(value==null)
			{
				return DBNull.Value;
			}
			else
			{
				return value;
			}
		}
		
		protected object ToModelValue(SqlDataReader reader,string columnName)
		{
			if(reader.IsDBNull(reader.GetOrdinal(columnName)))
			{
				return null;
			}
			else
			{
				return reader[columnName];
			}
		}
	}
}
